﻿using System;
using System.Windows.Forms;

namespace cubeAdjustment
{
    public partial class Gauss : Form
    {
        private void button2_Click(object sender, EventArgs e)
        {
            double XX = Convert.ToDouble(xx.Text);
            double YY = Convert.ToDouble(yy.Text);
            double HL;
            if (NN == 6)
            {
                HL = FromNum.SelectedIndex * 6.0 + 3;
            }
            else
            {
                HL = FromNum.SelectedIndex * 3.0 + 3;
            }
            if (radioButton1.Checked)
            {
                double bleta = XX / 6367558.496;

                double CSB2 = Math.Pow(Math.Cos(bleta), 2);

                double Bf = bleta + (50221746 + (293622 + (2350 + 22 * CSB2) * CSB2) * CSB2) * 0.0000000001 * Math.Sin(bleta) * Math.Cos(bleta);

                double CSBF2 = Math.Pow(Math.Cos(Bf), 2);

                double Nf = 6399698.902 - (21562.267 - (108.973 - 0.612 * CSBF2) * CSBF2) * CSBF2;
                double Z = YY / (Nf * Math.Cos(Bf));

                double b22 = (0.5 + 0.003369 * CSBF2) * Math.Sin(Bf) * Math.Cos(Bf);
                double b3 = 0.333333333 - (0.166666667 - 0.001123 * CSBF2) * CSBF2;
                double b4 = 0.25 + (0.16161 + 0.00562 * CSBF2) * CSBF2;
                double b5 = 0.2 - (0.166666667 - 0.0088 * CSBF2) * CSBF2;

                double BB_ = (Bf - (1 - (b4 - 0.12 * Z * Z) * Z * Z) * Z * Z * b22);
                double BB = BB_ * 180 / Math.PI;
                double BB2 = Math.Pow(Math.Cos(BB_), 2);
                double dtLa = (1 - (b3 - b5 * Z * Z) * Z * Z) * Z;
                double dtL = dtLa * 180 / Math.PI;
                double LL = dtL + HL;

                double gamma = (1 + ((0.33333 + 0.00674 * BB2) + (0.2 * BB2 - 0.0067) * dtLa * dtLa) * dtLa * dtLa * BB2) * dtLa * Math.Sin(BB_) * 180 / Math.PI;

                b0.Text = Math.Floor(BB).ToString();
                BB = (BB - Math.Floor(BB)) * 60;
                b1.Text = Math.Floor(BB).ToString();
                BB = (BB - Math.Floor(BB)) * 60;
                b2.Text = BB.ToString("0.0000");

                textBox5.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox4.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox3.Text = gamma.ToString("0.0000");

                l0.Text = Math.Floor(LL).ToString();
                LL = (LL - Math.Floor(LL)) * 60;
                l1.Text = Math.Floor(LL).ToString();
                LL = (LL - Math.Floor(LL)) * 60;
                l2.Text = LL.ToString("0.0000");
            }
            else
            {
                double bleta = XX / 6367452.133;

                double CSB2 = Math.Pow(Math.Cos(bleta), 2);

                double Bf = bleta + (50228976 + (293697 + (2383 + 22 * CSB2) * CSB2) * CSB2) * 0.0000000001 * Math.Sin(bleta) * Math.Cos(bleta);

                double CSBF2 = Math.Pow(Math.Cos(Bf), 2);
                double Nf = 6399596.652 - (21565.045 - (108.996 - 0.603 * CSBF2) * CSBF2) * CSBF2;

                double Z = YY / (Nf * Math.Cos(Bf));

                double b22 = (0.5 + 0.00336975 * CSBF2) * Math.Sin(Bf) * Math.Cos(Bf);
                double b3 = 0.3333333 - (0.166666667 - 0.001123 * CSBF2) * CSBF2;
                double b4 = 0.25 + (0.161612 + 0.005617 * CSBF2) * CSBF2;
                double b5 = 0.2 - (0.16666667 - 0.00878 * CSBF2) * CSBF2;

                double BB_ = (Bf - (1 - (b4 - 0.147 * Z * Z) * Z * Z) * Z * Z * b22);
                double BB = BB_ * 180 / Math.PI;
                double BB2 = Math.Pow(Math.Cos(BB_), 2);
                double dtLa = (1 - (b3 - b5 * Z * Z) * Z * Z) * Z;
                double dtL = dtLa * 180 / Math.PI;
                double LL = dtL + HL;


                double gamma = (1 + ((0.33332 + 0.00678 * BB2) + (0.2 * BB2 - 0.006667) * dtLa * dtLa) * dtLa * dtLa * BB2) * dtLa * Math.Sin(BB_) * 180 / Math.PI;

                b0.Text = Math.Floor(BB).ToString();
                BB = (BB - Math.Floor(BB)) * 60;
                b1.Text = Math.Floor(BB).ToString();
                BB = (BB - Math.Floor(BB)) * 60;
                b2.Text = BB.ToString("0.0000");

                textBox5.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox4.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox3.Text = gamma.ToString("0.0000");

                l0.Text = Math.Floor(LL).ToString();
                LL = (LL - Math.Floor(LL)) * 60;
                l1.Text = Math.Floor(LL).ToString();
                LL = (LL - Math.Floor(LL)) * 60;
                l2.Text = LL.ToString("0.0000");
            }
        }
        private int NN = 3;

        public Gauss()
        {
            InitializeComponent();
            _6.Checked = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                double B_ = Convert.ToDouble(b0.Text) + Convert.ToDouble(b1.Text) / 60 + Convert.ToDouble(b2.Text) / 3600;
                double L_ = Convert.ToDouble(l0.Text) + Convert.ToDouble(l1.Text) / 60 + Convert.ToDouble(l2.Text) / 3600;
                double L0;
                if (_6.Checked)
                {
                    L0 = FromNum.SelectedIndex * 6.0 + 3;
                }
                else
                {
                    L0 = FromNum.SelectedIndex * 3.0;
                }
                double B = B_ / 180 * Math.PI;

                double p = 180 / Math.PI * 3600;
                double BS = B * p;

                double l = (L_ - L0) / 180 * Math.PI;

                double CSB2 = Math.Pow(Math.Cos(B), 2);

                double gamma = (1 + ((0.33333 + 0.00674 * CSB2) + (0.2 * CSB2 - 0.0067) * l * l) * l * l * CSB2) * l * Math.Sin(B) * 180 / Math.PI;

                double L2 = Math.Pow(l, 2);
                double CS2 = Math.Pow(Math.Cos(B), 2);
                double N = 6399698.902 - (21562.267 - (108.973 - 0.612 * CS2) * CS2) * CS2;
                double a0 = 32140.4040 - (135.3302 - (0.7092 - 0.0040 * CS2) * CS2) * CS2;
                double a4 = (0.25 + 0.00252 * CS2) * CS2 - 0.0416666;
                double a6 = (0.1666666 * CS2 - 0.084) * CS2;
                double a3 = (0.3333333333 + 0.001123 * CS2) * CS2 - 0.1666666667;
                double a5 = 0.00833333 - (0.16666667 - (0.1968 + 0.0040 * CS2) * CS2) * CS2;

                double x = 6367558.4969 * BS / p - (a0 - (0.5 + (a4 + a6 * L2) * L2) * L2 * N) * Math.Sin(B) * Math.Cos(B);

                double y = (1 + (a3 + a5 * L2) * L2) * l * N * Math.Cos(B);

                textBox5.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox4.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox3.Text = gamma.ToString("0.0000");

                xx.Text = x.ToString("0.0000");
                yy.Text = y.ToString("0.0000");
            }
            else
            {
                double B_ = Convert.ToDouble(b0.Text) + Convert.ToDouble(b1.Text) / 60 + Convert.ToDouble(b2.Text) / 3600;
                double L_ = Convert.ToDouble(l0.Text) + Convert.ToDouble(l1.Text) / 60 + Convert.ToDouble(l2.Text) / 3600;
                double L0;
                if (_6.Checked)
                {
                    L0 = FromNum.SelectedIndex * 6.0 + 3;
                }
                else
                {
                    L0 = FromNum.SelectedIndex * 3.0;
                }

                double B = B_ / 180 * Math.PI;

                double p = 180 / Math.PI * 3600;
                double BS = B * p;

                double l = (L_ - L0) / 180 * Math.PI;

                double CSB2 = Math.Pow(Math.Cos(B), 2);

                double gamma = (1 + ((0.33332 + 0.00678 * CSB2) + (0.2 * CSB2 - 0.006667) * l * l) * l * l * l) * l * Math.Sin(B) * 180 / Math.PI;

                double L2 = Math.Pow(l, 2);
                double CS2 = Math.Pow(Math.Cos(B), 2);
                double N = 6399596.652 - (21565.045 - (108.996 - 0.603 * CS2) * CS2) * CS2;
                double a0 = 32144.5189 - (135.3646 - (0.7034 - 0.0041 * CS2) * CS2) * CS2;
                double a4 = (0.25 + 0.00253 * CS2) * CS2 - 0.04167;
                double a6 = (0.167 * CS2 - 0.083) * CS2;
                double a3 = (0.333333333 + 0.001123 * CS2) * CS2 - 0.1666666667;
                double a5 = 0.00878 - (0.1702 - 0.20382 * CS2) * CS2;

                double x = 6367452.1312 * BS / p - (a0 - (0.5 + (a4 + a6 * L2) * L2) * L2 * N) * Math.Sin(B) * Math.Cos(B);

                double y = (1 + (a3 + a5 * L2) * L2) * l * N * Math.Cos(B);

                textBox5.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox4.Text = Math.Floor(gamma).ToString();
                gamma = (gamma - Math.Floor(gamma)) * 60;
                textBox3.Text = gamma.ToString("0.0000");

                xx.Text = x.ToString("0.0000");
                yy.Text = y.ToString("0.0000");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            double XX = Convert.ToDouble(xx.Text);
            double YY = Convert.ToDouble(yy.Text);
            double HL, TL, BB, LL, dtLa;

            if (_6.Checked)
            {
                HL = FromNum.SelectedIndex * 6.0 + 3;
                TL = ToNum.SelectedIndex * 6.0 + 3;
            }
            else if (_3.Checked)
            {
                HL = FromNum.SelectedIndex * 3.0 + 3;
                TL = ToNum.SelectedIndex * 3.0 + 3;
            }
            else if (radioButton3.Checked)
            {
                HL = FromNum.SelectedIndex * 6.0 + 3;
                TL = ToNum.SelectedIndex * 3.0 + 3;
            }
            else
            {
                HL = FromNum.SelectedIndex * 3.0 + 3;
                TL = ToNum.SelectedIndex * 6.0 + 3;
            }


            if (radioButton1.Checked)
            {
                double bleta = XX / 6367558.496;

                double CSB2 = Math.Pow(Math.Cos(bleta), 2);

                double Bf = bleta + (50221746 + (293622 + (2350 + 22 * CSB2) * CSB2) * CSB2) * 0.0000000001 * Math.Sin(bleta) * Math.Cos(bleta);

                double CSBF2 = Math.Pow(Math.Cos(Bf), 2);

                double Nf = 6399698.902 - (21562.267 - (108.973 - 0.612 * CSBF2) * CSBF2) * CSBF2;
                double Z = YY / (Nf * Math.Cos(Bf));

                double b22 = (0.5 + 0.003369 * CSBF2) * Math.Sin(Bf) * Math.Cos(Bf);
                double b3 = 0.333333333 - (0.166666667 - 0.001123 * CSBF2) * CSBF2;
                double b4 = 0.25 + (0.16161 + 0.00562 * CSBF2) * CSBF2;
                double b5 = 0.2 - (0.166666667 - 0.0088 * CSBF2) * CSBF2;


                double BB_ = (Bf - (1 - (b4 - 0.12 * Z * Z) * Z * Z) * Z * Z * b22);
                BB = BB_ * 180 / Math.PI;
                dtLa = (1 - (b3 - b5 * Z * Z) * Z * Z) * Z;
                double dtL = dtLa * 180 / Math.PI;
                LL = dtL + HL;


            }
            else
            {
                double bleta = XX / 6367452.133;

                double CSB2 = Math.Pow(Math.Cos(bleta), 2);

                double Bf = bleta + (50228976 + (293697 + (2383 + 22 * CSB2) * CSB2) * CSB2) * 0.0000000001 * Math.Sin(bleta) * Math.Cos(bleta);

                double CSBF2 = Math.Pow(Math.Cos(Bf), 2);
                double Nf = 6399596.652 - (21565.045 - (108.996 - 0.603 * CSBF2) * CSBF2) * CSBF2;

                double Z = YY / (Nf * Math.Cos(Bf));

                double b22 = (0.5 + 0.00336975 * CSBF2) * Math.Sin(Bf) * Math.Cos(Bf);
                double b3 = 0.3333333 - (0.166666667 - 0.001123 * CSBF2) * CSBF2;
                double b4 = 0.25 + (0.161612 + 0.005617 * CSBF2) * CSBF2;
                double b5 = 0.2 - (0.16666667 - 0.00878 * CSBF2) * CSBF2;

                double BB_ = (Bf - (1 - (b4 - 0.147 * Z * Z) * Z * Z) * Z * Z * b22);
                BB = BB_ * 180 / Math.PI;
                dtLa = (1 - (b3 - b5 * Z * Z) * Z * Z) * Z;
                double dtL = dtLa * 180 / Math.PI;
                LL = dtL + HL;

            }

            if (radioButton1.Checked)
            {
                double B_ = BB;
                double L_ = LL;

                double B = B_ / 180 * Math.PI;

                double p = 180 / Math.PI * 3600;
                double BS = B * p;

                double l = (L_ - TL) / 180 * Math.PI;

                double L2 = Math.Pow(l, 2);
                double CS2 = Math.Pow(Math.Cos(B), 2);
                double N = 6399698.902 - (21562.267 - (108.973 - 0.612 * CS2) * CS2) * CS2;
                double a0 = 32140.4040 - (135.3302 - (0.7092 - 0.0040 * CS2) * CS2) * CS2;
                double a4 = (0.25 + 0.00252 * CS2) * CS2 - 0.041666;
                double a6 = (0.166666 * CS2 - 0.084) * CS2;
                double a3 = (0.333333333 + 0.001123 * CS2) * CS2 - 0.1666666667;
                double a5 = 0.0083333 - (0.16666667 - (0.1968 + 0.0040 * CS2) * CS2) * CS2;

                double x = 6367558.4969 * BS / p - (a0 - (0.5 + (a4 + a6 * L2) * L2) * L2 * N) * Math.Sin(B) * Math.Cos(B);
                double y = (1 + (a3 + a5 * L2) * L2) * l * N * Math.Cos(B);

                textBox2.Text = x.ToString("0.0000");
                textBox1.Text = y.ToString("0.0000");
            }
            else
            {
                double B_ = BB;
                double L_ = LL;

                double B = B_ / 180 * Math.PI;

                double p = 180 / Math.PI * 3600;
                double BS = B * p;

                double l = (L_ - TL) / 180 * Math.PI;

                double L2 = Math.Pow(l, 2);
                double CS2 = Math.Pow(Math.Cos(B), 2);
                double N = 6399596.652 - (21565.045 - (108.996 - 0.603 * CS2) * CS2) * CS2;
                double a0 = 32144.5189 - (135.3646 - (0.7034 - 0.0041 * CS2) * CS2) * CS2;
                double a4 = (0.25 + 0.00253 * CS2) * CS2 - 0.04167;
                double a6 = (0.167 * CS2 - 0.083) * CS2;
                double a3 = (0.33333 + 0.001123 * CS2) * CS2 - 0.1666666667;
                double a5 = 0.00878 - (0.1702 - 0.20382 * CS2) * CS2;

                double x = 6367452.1378 * BS / p - (a0 - (0.5 + (a4 + a6 * L2) * L2) * L2 * N) * Math.Sin(B) * Math.Cos(B);
                double y = (1 + (a3 + a5 * L2) * L2) * l * N * Math.Cos(B);

                textBox2.Text = x.ToString("0.0000");
                textBox1.Text = y.ToString("0.0000");
            }

            b0.Text = Math.Floor(BB).ToString();
            BB = (BB - Math.Floor(BB)) * 60;
            b1.Text = Math.Floor(BB).ToString();
            BB = (BB - Math.Floor(BB)) * 60;
            b2.Text = BB.ToString("0.0000");

            l0.Text = Math.Floor(LL).ToString();
            LL = (LL - Math.Floor(LL)) * 60;
            l1.Text = Math.Floor(LL).ToString();
            LL = (LL - Math.Floor(LL)) * 60;
            l2.Text = LL.ToString("0.0000");
        }

        private void groupBox4_Leave(object sender, EventArgs e)
        { 
        string[] obj120, obj60;
            obj120 = new string[120];
            obj60 = new string[60];


            for (int i = 0; i < 120; i++)
            {
                obj120[i] = (i + 1).ToString() + "/" + (3 * (i + 1)).ToString() + "°";
            }

            for (int i = 0; i < 60; i++)
            {
                obj60[i] = (i + 1).ToString() + "/" + (6 * i + 3).ToString() + "°";
            }

            if (_6.Checked)
            {
                NN = 6;
                FromNum.Items.Clear();
                FromNum.Items.AddRange(obj60);
                ToNum.Items.Clear();
                ToNum.Items.AddRange(obj60);
            }
            else if (_3.Checked)
            {
                NN = 3;
                FromNum.Items.Clear();
                FromNum.Items.AddRange(obj120);
                ToNum.Items.Clear();
                ToNum.Items.AddRange(obj120);
            }
            else if (radioButton3.Checked)
            {
                NN = 63;
                FromNum.Items.Clear();
                FromNum.Items.AddRange(obj60);
                ToNum.Items.Clear();
                ToNum.Items.AddRange(obj120);
            }
            else
            {
                NN = 3;
                ToNum.Items.Clear();
                ToNum.Items.AddRange(obj60);
                FromNum.Items.Clear();
                FromNum.Items.AddRange(obj120);
            }
        }
    }
}
